<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\DB\Query;
use think\Request;
use think\Session;
header("Content-type: text/html; charset=utf-8");
class User extends Controller
{
	public function welcome()
	{
		return '讯都欢迎你';
	}

	public function index()
	{
		return $this->fetch('auth/index');
	}

    public function login()
    {
       	$date = input()['name'];
    	$dats = input()['pass'];
	   	$list = db('admin')
				    ->where('username', $date)
				    ->find();
		if($list == "" ){
		  	return $this->error('账号不存在！');
		}else if($list['password'] != md5($dats)){
		  	return $this->error('密码错误！');
		}else{
		    // Session::set('',$list);
			return $this->success('登陆成功！',url('admin/User/index'));
		}
    }  

    public function xin()
    {
    	$main = DB('news')->field('id,title,areaid,type_id,status,is_hot,information,recommended')->select();
        $citys = db('area')->field('areaid,areaname')->where('letter_id','neq','')->select();
        // dump($citys);
        $type = DB('cat')->select();
        $this->assign('type',$type);
        $this->assign('citys',$citys);
    	$this->assign('main', $main);
    	return $this->fetch('auth/list');
    }  

    public function shen()
    {
        $uid = input('post.uid');
        // return $uid;
        $list = Db('news')->where('id',$uid)->field('status')->find();
           if($list['status'] == '0'){
            Db('news')->where('id',$uid)->update(['status'=>'1']);
           }else{
            Db('news')->where('id',$uid)->update(['status'=>'0']);
           }  
    }

    public function hot()
    {
        $uid = input('post.hid');
        // return $uid;
        $list = Db('news')->where('id',$uid)->field('is_hot')->find();
           if($list['is_hot'] == '0'){
            Db('news')->where('id',$uid)->update(['is_hot'=>'1']);
           }else{
            Db('news')->where('id',$uid)->update(['is_hot'=>'0']);
           }  
    }

    public function recom()
    {
        $uid = input('post.rid');
        // return $uid;
        $list = Db('news')->where('id',$uid)->field('recommended')->find();
           if($list['recommended'] == '0'){
            Db('news')->where('id',$uid)->update(['recommended'=>'1']);
           }else{
            Db('news')->where('id',$uid)->update(['recommended'=>'0']);
           }  
    }

    public function infor()
    {
        $uid = input('post.fid');
        // return $uid;
        $list = Db('news')->where('id',$uid)->field('information')->find();
           if($list['information'] == '0'){
            Db('news')->where('id',$uid)->update(['information'=>'1']);
           }else{
            Db('news')->where('id',$uid)->update(['information'=>'0']);
           }  
    }

    public function area()
    {
        //省会id
        $id = input('post.tid');
        $city=DB('area')->where(['parentid'=>$id])->select();
        return $city;
    }

    public function add()
    {
        $sheng=DB('area')->where('parentid',0)->field('areaid,areaname')->select();
        // dump($sheng);die;
        $this->assign('sheng',$sheng);
        return $this->fetch('auth/add');
    }

    public function wenedit($id)
    {
        $pe = DB('news')
            ->alias('a')
            ->join('news_data b','a.id=b.news_id')
            ->where('a.id',$id)->select();
            // dump($pe);die;
        $typeattr=DB('cat')->select();
        $this->assign('typeattr',$typeattr);
        $this->assign('pe',$pe);
        return $this->fetch('auth/edit');
    }

    public function edit(Request $request)
    {
        $val = $request->param();
        // dump($val);die;
        $id=$val['uid'];
        $line=[
            'title'=>$val['title'],
            'type_id'=>$val['select']
        ];
        $date=[
            'content'=>$val['content']
        ];
        
        if(Db('news')
            ->where('id', $id)
            ->update($line) || DB('news_data')->where('news_id',$id)->update($date)){
                return $this->success('修改成功','admin/User/xin');
            }else{
                return $this->error('修改失败');
            }
    }

    public function del($id)
    {
        
        if(Db('news')->where('id',$id)->delete() && Db('news_data')->where('news_id',$id)->delete()){
             return $this->success('删除成功','admin/User/xin');
         }else{
            return $this->error('删除失败');
         }
    }

    public function created(Request $request)
    {
        $val = $request->param();
        // dump($val);die;
        // $biao=$val['biao'];
        $pas = $val['pas'];
        $a=rand(1,6);
        if(!empty($val['city'])){
            $cityid=$val['city'];
            $data=[
            'area_id'=>$cityid,
            'username'=>$val['name'],
            'password'=>md5($pas),
            'salt'=>md5($a)
        ];
        }else{
            $data=[
            'area_id'=>0,
            'username'=>$val['name'],
            'password'=>md5($pas),
            'salt'=>md5($a)
        ];
    }
         DB('admin')->insert($data);
        return $this->success('添加成功','admin/User/xin');
        
        // dump($a);die;
        
        // $city = DB('xd_area')->where('areaid',$cityid)->field('areaname')->find();
                // dump($city['areaname']);die;
        // $table = $biao."_news";
        // dump($table);die;
    //     $sql = "CREATE TABLE IF NOT EXISTS `$table` (
    //     `id` int(30) unsigned NOT NULL,
    //     `title` varchar(50) DEFAULT NULL,
    //     `superior_id` int(10) DEFAULT NULL COMMENT '一级栏目id',
    //     `type_id` int(10) DEFAULT NULL COMMENT '分类id',
    //     `areaid` int(10) DEFAULT NULL COMMENT '地区的id',
    //     `pic` char(100) DEFAULT NULL COMMENT '图片',
    //     `pubtime` char(20) DEFAULT NULL,
    //     `status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '0：审核成功 1：审核失败',
    //     `recommended` tinyint(2) NOT NULL DEFAULT '0' COMMENT '0：不推荐 1：推荐',
    //     `topic` tinyint(2) NOT NULL DEFAULT '0' COMMENT '今日话题 0：是 1：否',
    //     `is_hot` tinyint(2) NOT NULL DEFAULT '0' COMMENT '0：不热门  1：热门',
    //     `information` tinyint(2) NOT NULL DEFAULT '0' COMMENT '今日资讯  0：是  1：否',
    //     `is_pic` tinyint(2) NOT NULL DEFAULT '0' COMMENT '0:调用 1:不调用',
    //     `duction` varchar(255) DEFAULT NULL COMMENT '标题简介',
    //     INDEX (title)
    //     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='活跃统计' AUTO_INCREMENT=1 ;";
    //     Db::execute($sql);
    //     $src = '../create' ;//模板
    //     $des = '../'."$biao" ;//济南新闻的文件夹
    //     $this->recurse_copy($src,$des);
    //     //读取文件
    //     $f=$des."/conn.php";
    //     file_put_contents($f,str_replace('city',$table,file_get_contents($f)));
    //     file_put_contents($f,str_replace('shi',$city['areaname'],file_get_contents($f)));
    // }

    // function recurse_copy($src,$des){
    //         $dir = opendir($src);
    //         @mkdir($des);
    //         while(false !== ( $file = readdir($dir)) ) {
    //                  if (( $file != '.' ) && ( $file != '..' )) {
    //                         if ( is_dir($src . '/' . $file) ) {
    //                             recurse_copy($src . '/' . $file,$des . '/' . $file);
    //                         } else {
    //                             copy($src . '/' . $file,$des . '/' . $file);
    //                         }
    //                 }
    //           }
    //         closedir($dir);
        }
}
